package com.ruoyi.web.controller.formwork;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.formwork.domain.BaseFormWork;
import com.ruoyi.formwork.domain.OperationFormWork;
import com.ruoyi.formwork.service.OperationFormWorkService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;


@RestController
@RequestMapping("/OperationFormWorkController")
public class OperationFormController {

    @Resource
    private OperationFormWorkService OperationFormWorkService;
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[]{"admin"});
    }

    @Log(title = "运维经验查询", businessType = BusinessType.QUERY)
    @PostMapping("/queryOperationFormWork")
    public AjaxResult queryOperationFormWork(@RequestBody OperationFormWork operationFormWork)
    {
        List<OperationFormWork> OperationFormWorks = OperationFormWorkService.queryOperationFormWork(operationFormWork);
        return success(OperationFormWorks);
    }

    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperationFormWork operationFormWork)
    {
        List<OperationFormWork> OperationFormWorks = OperationFormWorkService.queryOperationFormWork(operationFormWork);

        ExcelUtil<OperationFormWork> util = new ExcelUtil<OperationFormWork>(OperationFormWork.class);

        util.exportExcel(response, OperationFormWorks, "经验模板");
    }
    @Log(title = "运维经验删除", businessType = BusinessType.DELETE)
    @PostMapping("/deleteOperationFormWork")
    public AjaxResult deleteOperationFormWork(@RequestBody OperationFormWork operationFormWork)
    {
        boolean b = OperationFormWorkService.deleteOperationFormWork(operationFormWork);
        if(b){
            return success("删除成功！");
        }else{
            return success("删除失败！");
        }

    }
    @Log(title = "运维经验新增", businessType = BusinessType.INSERT)
    @PostMapping("/addOperationFormWork")
    public AjaxResult addOperationFormWork(@RequestBody OperationFormWork operationFormWork)
    {
        boolean b = OperationFormWorkService.save(operationFormWork);
        if(b){
            return success("新增成功！");
        }else{
            return success("新增失败！");
        }

    }
    @Log(title = "运维经验修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateOperationFormWork")
    public AjaxResult updateOperationFormWork(@RequestBody OperationFormWork operationFormWork)
    {
        boolean b = OperationFormWorkService.updateById(operationFormWork);
        if(b){
            return success("修改成功！");
        }else{
            return success("修改失败！");
        }

    }

    @PostMapping("/queryOperationFormWorkById")
    public AjaxResult queryOperationFormWorkById(@RequestBody OperationFormWork operationFormWork)
    {
        OperationFormWork model = OperationFormWorkService.getById(operationFormWork.getId());
        return success(model);

    }
}
